package com.citai.tool;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class Utils {
	// 获取汉字首字母
		public static String toHanyuPinyinAcronym(String ChineseLanguage){
			char[] cl_chars = ChineseLanguage.trim().toCharArray();
			String hanyupinyin = "";
			HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
			defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
			defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
			defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ;
			try {
				for (int i=0; i<cl_chars.length; i++){
					if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")){// 如果字符是中文,则将中文转为汉语拼音
						String[] temp = PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat);
						hanyupinyin += temp[0].substring(0,1);
						// hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
					} else if(cl_chars[i] == 12288){
						hanyupinyin += (char) 32;
					} else if(cl_chars[i] > 65280 && cl_chars[i] < 65375){
						hanyupinyin += (char) (cl_chars[i] - 65248);
					}else {// 如果字符不是中文,则不转换
						hanyupinyin += cl_chars[i];
					}
				}
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				System.out.println("字符不能转成汉语拼音");
			}
			return hanyupinyin.toUpperCase();
		}
		
		/**
		    转半角的函数(DBC case)<br/><br/>
		    全角空格为12288，半角空格为32
		    其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
	    * @param input 任意字符串
	    * @return 半角字符串
	    *
	    */
	   public static String ToDBC(String input) {
	       char[] c = input.toCharArray();
	       for (int i = 0; i < c.length; i++) {
	           if (c[i] == 12288) {
	               //全角空格为12288，半角空格为32
	               c[i] = (char) 32;
	               continue;
	           }
	           if (c[i] > 65280 && c[i] < 65375)
	               //其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
	               c[i] = (char) (c[i] - 65248);
	       }
	       return new String(c);
	   }

		public static void main(String[] args) {
			String s = toHanyuPinyinAcronym("你好，中国！！！");
			System.out.println(s);
			
			char c = (char)37922;
			System.out.println(c);
		}
}
